﻿namespace Microsoft.SharePoint.Client.WebParts
{
    using Microsoft.SharePoint.Client;
    using System;
    using System.ComponentModel;

    [ScriptType("SP.WebParts.LimitedWebPartManager", ServerTypeId="{ac641ade-62df-49c9-af8e-abda6278e920}")]
    public class LimitedWebPartManager : ClientObject
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public LimitedWebPartManager(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [Remote]
        public WebPartDefinition AddWebPart(WebPart webPart, string zoneId, int zoneIndex)
        {
            if (base.Context.ValidateOnClient)
            {
                if (webPart == null)
                {
                    throw ClientUtility.CreateArgumentNullException("webPart");
                }
                if (zoneId == null)
                {
                    throw ClientUtility.CreateArgumentNullException("zoneId");
                }
                if ((zoneId != null) && (zoneId.Length == 0))
                {
                    throw ClientUtility.CreateArgumentException("zoneId");
                }
                if ((zoneId != null) && (zoneId.Length > 0x40))
                {
                    throw ClientUtility.CreateArgumentException("zoneId");
                }
                if (zoneIndex < 0)
                {
                    throw ClientUtility.CreateArgumentException("zoneIndex");
                }
            }
            WebPartDefinition definition = new WebPartDefinition(base.Context, new ObjectPathMethod(base.Context, base.Path, "AddWebPart", new object[] { webPart, zoneId, zoneIndex }));
            definition.Path.SetPendingReplace();
            ObjectIdentityQuery query = new ObjectIdentityQuery(definition.Path);
            base.Context.AddQueryIdAndResultObject(query.Id, definition);
            base.Context.AddQuery(query);
            return definition;
        }

        [Remote]
        public WebPartDefinition ImportWebPart(string webPartXml)
        {
            if (base.Context.ValidateOnClient)
            {
                if (webPartXml == null)
                {
                    throw ClientUtility.CreateArgumentNullException("webPartXml");
                }
                if ((webPartXml != null) && (webPartXml.Length == 0))
                {
                    throw ClientUtility.CreateArgumentException("webPartXml");
                }
            }
            WebPartDefinition definition = new WebPartDefinition(base.Context, new ObjectPathMethod(base.Context, base.Path, "ImportWebPart", new object[] { webPartXml }));
            definition.Path.SetPendingReplace();
            ObjectIdentityQuery query = new ObjectIdentityQuery(definition.Path);
            base.Context.AddQueryIdAndResultObject(query.Id, definition);
            base.Context.AddQuery(query);
            return definition;
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (!flag)
            {
                string str = peekedName;
                if (str == null)
                {
                    return flag;
                }
                if (!(str == "Scope"))
                {
                    if (str == "WebParts")
                    {
                        flag = true;
                        reader.ReadName();
                        this.WebParts.FromJson(reader);
                        return flag;
                    }
                    if (str == "HasPersonalizedParts")
                    {
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["HasPersonalizedParts"] = reader.ReadBoolean();
                    }
                    return flag;
                }
                flag = true;
                reader.ReadName();
                base.ObjectData.Properties["Scope"] = reader.ReadEnum<PersonalizationScope>();
            }
            return flag;
        }

        [Remote]
        public bool HasPersonalizedParts
        {
            get
            {
                base.CheckUninitializedProperty("HasPersonalizedParts");
                return (bool) base.ObjectData.Properties["HasPersonalizedParts"];
            }
        }

        [Remote]
        public PersonalizationScope Scope
        {
            get
            {
                base.CheckUninitializedProperty("Scope");
                return (PersonalizationScope) base.ObjectData.Properties["Scope"];
            }
        }

        [Remote]
        public WebPartDefinitionCollection WebParts
        {
            get
            {
                object obj2;
                if (base.ObjectData.ClientObjectProperties.TryGetValue("WebParts", out obj2))
                {
                    return (WebPartDefinitionCollection) obj2;
                }
                WebPartDefinitionCollection definitions = new WebPartDefinitionCollection(base.Context, new ObjectPathProperty(base.Context, base.Path, "WebParts"));
                base.ObjectData.ClientObjectProperties["WebParts"] = definitions;
                return definitions;
            }
        }
    }
}

